package com.hellogood.yaohuo.server.serviceImpl;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import com.hellogood.yaohuo.server.mgr.factory.MgrFactory;
import com.hellogood.yaohuo.shared.entity.Form;
import com.hellogood.yaohuo.shared.entity.Ziyou;
import com.hellogood.yaohuo.shared.entity.ZyForm;
import com.hellogood.yaohuo.shared.services.ZiyouService;

public class ZiyouServiceImpl extends RemoteServiceServlet implements ZiyouService {

	/**
	 * 
	 */
	private static final long serialVersionUID = 5450866300943420555L;

	@Override
	public List<Ziyou> getAllZiyou() {
		return MgrFactory.getInstance().createZiyouMgr().getAllZiyou();
	}

	@Override
	public String createZiyou(Ziyou zy) {
		zy.setZiyouCode(getMaxCode());
		return MgrFactory.getInstance().createZiyouMgr().addNewZiyou(zy);
	}

	@Override
	public String modifyZiyou(Ziyou zy) {
		return MgrFactory.getInstance().createZiyouMgr().ModifyZiyou(zy);
	}

	@Override
	public String getMaxCode() {
		DecimalFormat df = new DecimalFormat("000");
		String no = MgrFactory.getInstance().createZiyouMgr().getLastCode();
		if (no != null) {
			// 序号为最后两位 格式为"WLYH组织号YYYYMMDDXX" XX为序号
			int lastCode = 0;
			if (no.length() == 3) {
				lastCode = Integer.valueOf(no);
				lastCode++;
				return df.format(lastCode);
			} else {
				return "001";
			}
		}
		return "001";

	}

	@Override
	public String saveForm(ZyForm form, boolean isModify) {
		if (isModify) {
			return MgrFactory.getInstance().createZiyouMgr().modifyZiyouForm(form);
		} else {
			return MgrFactory.getInstance().createZiyouMgr().createZiyouForm(form);
		}
	}


	@Override
	public Form getFormDetail(String billNo) {
		return MgrFactory.getInstance().createZiyouMgr().getFormDetail(billNo);
	}

	@Override
	public String deleteForm(String billNo) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String bankForm(List<String> bills) {
		return MgrFactory.getInstance().createZiyouMgr().rz(bills);
	}

	public String getLastZyBillNo() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
		DecimalFormat df = new DecimalFormat("0000");
		String billNo = "ZYBH" + sdf.format(new Date());
		int lastNo = MgrFactory.getInstance().createZiyouMgr().getLastZyBillNo(billNo);
		lastNo++;
		billNo += df.format(lastNo);
		return billNo;
	}

	@Override
	public List<Form> searchZiyouFormByOrgCodeAndDate(String orgCode, int level, String startDate, String endDate, String rz) {
		return MgrFactory.getInstance().createZiyouMgr().searchZyForm(orgCode, level, startDate, endDate, rz);
	}
}
